/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Created on : Jan 12, 2011, 12:11:15 PM
 * @author diogo
 */

function AjaxConnection(url){
    var theSuper = this;
    /**
     * function to get whatever the shit we need for ajax
     */
    this.GetXmlHttpObject = function (){
        if (window.XMLHttpRequest){
            return new XMLHttpRequest();
        }
        if (window.ActiveXObject){
            return new ActiveXObject("Microsoft.XMLHTTP");
        }
        return null;
    }

    this.xmlhttp = this.GetXmlHttpObject();
    this.responseText = this.xmlhttp.responseText
    // the url to connect, doesnt need the key/value set
    this.url= url;
    this.Values_tb = [];

    /**
     * starts the ajax
     */
    this.startConnection = function (){
        theSuper.xmlhttp=theSuper.GetXmlHttpObject();
        if (theSuper.xmlhttp==null){
            alert ("Your browser does not support Ajax HTTP");
            return;
        }
        //alert(theSuper.getAction());
        //alert(this.makeUrl());

		theSuper.SetLoading();
		//*/
        theSuper.xmlhttp.onreadystatechange=theSuper.getAction();
        theSuper.xmlhttp.open("GET",this.makeUrl(),true);
        theSuper.xmlhttp.send(null);
    }


    /**
     * set where to put the output from the servlet <BR>
     * <b>note:</b> use the <i>getElementById</i> method and we will put the content at it's
     * <i>innerHTML</i>
     */
    this.setOutput = function(dom){
        theSuper.output = dom;
    }

    /**
     * this is to set the callback function
     * @param aFunction - the command to execute
     */
    this.setAction = function(aFunction) {
        //default execution
        if (aFunction == null) {
            aFunction = 
            function() {
                theSuper.output.innerHTML = theSuper.xmlhttp.responseText;
				
            }
        }

        theSuper.action =
        function(){
            if (theSuper.xmlhttp.readyState==4) {
                aFunction();
				theSuper.OnResponseReceive();
            }
        }
        
    }

    /**
     * this is the callback
     */
    this.getAction = function () {
        //alert(theSuper.xmlhttp.readyState)
        if (theSuper.action == null){
            theSuper.setAction();
        }
        return theSuper.action;
    }




    /**
     * Sets or replace an key-value relation of servlet connection
     * @param sKey - the parameter's name
     * @param sValue - the parameter's value
     */
    this.setParameter = function(sKey, sValue){
        theSuper.Values_tb[sKey] = sValue;
    }
    
    this.clearParameters = function(){
        theSuper.Values_tb = null;
    }

    this.makeUrl = function(){
        var output = theSuper.url;
        var sParameters = "";
        var bHasParams = false;
        var count = 0;
        for (var k in theSuper.Values_tb){
            count++;
            //alert(k)
            bHasParams = true;
            sParameters += k + "=" + theSuper.Values_tb[k]  + "&";
        }
        if (bHasParams){
            //says this shit has parameters
            output +="?";
            //removes the last param
            sParameters = sParameters.substring(0, sParameters.length-1)
            output +=sParameters;
        }
        return output;
        
        
    }

	/**
	 * set a loading gif image at the set output.
	 */
	this.SetLoading = function(){

		//* // @ TODO: test if here we can put a 'processing' feedback
		//theSuper.output.innerHTML = "perae... (consultando banco)";
		theSuper.output.innerHTML = "<img src=\"images/ajax-loader.gif\" alt=\"loader\"/>";
	}

	/**
	 * this function is called whenever receive a response from the server.
	 * feel free to override this function.
	 */
	this.OnResponseReceive = function (){
		
	}
}
